/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package palms.client.android.apis.view;

import java.io.IOException;
import java.net.MalformedURLException;

import palms.client.android.apis.services.LocationService;
import palms.client.android.overlay.ResultsOverlay;
import palms.client.android.overlay.UserLocation;
import palms.client.domain.User;
import palms.client.manager.ClientManager;
import palms.location.results.ALocation;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.example.android.google.apis.R;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;


public class PalmsMapView extends MapActivity {
	
	private static User user;
	private static ClientManager clientManager;
	private Intent locating;
	private UserLocation ul;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.mapview);
        
        LocationService.setActivity(this);
		this.locating = new Intent(this, LocationService.class);
		startService(locating);
		
		MapView mapView = (MapView) this.findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		this.ul = new UserLocation(this.getApplicationContext(), mapView, user);
		ResultsOverlay ro = new ResultsOverlay(user, this);
			
		mapView.getOverlays().add(ul);
		mapView.getOverlays().add(ro);
    }
    
    @Override
    protected void onResume(){
    	super.onResume();
    	this.ul.enableMyLocation();
    }
    
    @Override
    protected void onStop(){
    	super.onStop();
    	this.ul.disableMyLocation();
    }
    
    public void CallPlaceInfo(ALocation l) throws MalformedURLException, IOException{
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setCancelable(true);
    	builder.setTitle(l.getName());
    	builder.setInverseBackgroundForced(false);
    	builder.setMessage("Address: " + l.getAddress());
    	builder.setIcon(Drawable.createFromStream(((java.io.InputStream)new java.net.URL(l.getImgUrl()).getContent()), "icon"));
    	
    	builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    	  public void onClick(DialogInterface dialog, int which) {
    	    dialog.dismiss();
    	  }
    	});
    	
    	AlertDialog alert = builder.create();
    	alert.show();

    }
    
    @Override
    protected boolean isRouteDisplayed() { return false; }
  
    public void onSelectService(View v){
    	SelectService.setPalmsMapView(this);
		Intent newIntent = new Intent(this, SelectService.class);
		startActivityForResult(newIntent, 0);
		stopService(locating);
    }

	public static User getUser() {
		return user;
	}

	public static void setUser(User auser) {
		user = auser;
	}
	
	public static void setClientManager(ClientManager cm) {
		clientManager = cm;
	}
	
	public ClientManager getClientManager(){
		return clientManager;
	}
	
	public User getNSUser(){
		return user;
	}

}
